package com.aizhixin.test.logonAndlAnd.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aizhixin.test.logonAndlAnd.dto.userImformationDTO;
import com.aizhixin.test.question.dto.bestKonlege;

import aj.org.objectweb.asm.Type;

@Component
public class userImformationDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**  
	 *<p>根据opengid查询是否该微信用户注册过</p> 
	  * 判断这个token值和OpenId在数据库里面是否有，有则数据，无则空
	 * @Title: findUser    
	 * @version:V0.1     
	 * @param openid
	 * @return:List<User>
	 */
	public boolean findUser(String openid) {
	RowMapper<userImformationDTO> rowMapper = new BeanPropertyRowMapper<userImformationDTO>(userImformationDTO.class);
	String sql = "SELECT q.OPENID,q.BAIDU_TOKEN  FROM user_imformation as q WHERE q.OPENID=?";
	 List<userImformationDTO> list = jdbcTemplate.query(sql, rowMapper,openid);
	 if (0 == list.size()) {
	return true;//找不到
	}
	 else {
	return false;//找得到
	} 
	}
	/*
	 * userdto里面清空用户除了userid和openId和token以外的其他值
	 * 以区别注册和未注册
	 */
	public userImformationDTO cleanuserDTO(userImformationDTO dto) {
		dto.setCity(null);
		dto.setCode(null);
		dto.setUserName(null);
		dto.setProvince(null);
		dto.setHeadSculpure(null);
		dto.setNationality(null);
		return dto;
	}

	/*
	 * 有则完成了，到登陆界面，将刚刚的token值返回给前端
	 * 返回值有token还有openId
	 */
	/*
	 *无则调用这个存数据注册
	 */
	public int reUser(userImformationDTO dto) {
		int number=-1;
		String sql = "INSERT INTO  user_imformation (OPENID,HEAD_SCULPTURE,USER_NAME,NATIONALITY,PROVINCE,CITY,CREATED_DATE) VALUES (?,?,?,?,?,?,now())";
		jdbcTemplate.update(sql,dto.getOpenId(),dto.getHeadSculpure(),dto.getUserName(),dto.getNationality(),dto.getProvince(),dto.getCity());
		number=1;
		return number;
		}
	/*在人脸识别之后改变token/赋值
	 * UPDATE user_imformation SET BAIDU_TOKEN=? WHERE OPENID=?
	 */
	public int updateToken(userImformationDTO dto) {
		int a=-1;
		String sql="UPDATE user_imformation SET BAIDU_TOKEN=? WHERE OPENID=?";
		jdbcTemplate.update(ConcatExpression ->{
			PreparedStatement ps =ConcatExpression.prepareStatement(sql);
			ps.setString(1,dto.getToken());
			ps.setString(2, dto.getOpenId());
			return ps;
			});
		a=1;
		return a;
	}
/*
 * SELECT q.OPENID,q.HEAD_SCULPTURE,q.NATIONALITY,q.PROVINCE,q.CITY,q.USER_NAME,q.CREATED_DATE,q.BAIDU_TOKEN FROM user_imformation as q WHERE q.OPENID=?
 * 
 */
	
		public userImformationDTO findByOpenId(String openid) {
			RowMapper<userImformationDTO> rowMapper=new BeanPropertyRowMapper<userImformationDTO>(userImformationDTO.class);
			String sql = "SELECT q.OPENID,q.HEAD_SCULPTURE,q.NATIONALITY,q.PROVINCE,q.CITY,q.USER_NAME,q.CREATED_DATE,q.BAIDU_TOKEN FROM user_imformation as q WHERE q.OPENID=?";
			return jdbcTemplate.queryForObject(sql, rowMapper, openid);
			}

/*
 * SELECT q.ID FROM user_imformation as q WHERE q.OPENID=2;
 */
	public Long findIdByOpenId(String openId) {

		final String sql = "SELECT q.ID FROM user_imformation as q WHERE q.OPENID=?";
		List<userImformationDTO> list = jdbcTemplate.query(sql, new Object[] {openId},
				new RowMapper<userImformationDTO>() {
					public userImformationDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
						userImformationDTO test = new userImformationDTO();
						test.setUserId(rs.getLong("ID"));
						return test;
					}
				});
		if(list==null||list.size()==0){
			return null;
		}
		else {
			return list.get(0).getUserId();	
		}
		

	}
	/*
	 * SELECT q.BAIDU_TOKEN FROM user_imformation as q WHERE q.OPENID=2;
	 */
	public String findtokenByOpenId(String openId) {

		final String sql = "SELECT q.BAIDU_TOKEN FROM user_imformation as q WHERE q.OPENID=?";
		List<userImformationDTO> list = jdbcTemplate.query(sql, new Object[] { openId },
				new RowMapper<userImformationDTO>() {
					public userImformationDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
						userImformationDTO test = new userImformationDTO();
						test.setToken(rs.getString("BAIDU_TOKEN"));
						return test;
					}
				});
		if(list==null||list.size()==0) {
			return null;
		}
		else {
			return list.get(0).getToken();	
		}
		

	}
	/*
	 * 查用户的注册日期算用户来到我们这个app多少天了，纪念意义
	 */
}


